//
//  WDDFinancialTableViewCell.m
//  SZBank
//
//  Created by rimi on 16/7/8.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "WDDFinancialTableViewCell.h"
#import "NoticeTableViewCell.h"
#import "WDDFlipView.h"
#import "WDDProductFlipView.h"
#import "WDDImageScrollView.h"
#define IMAGEURL @"http://117.172.47.171:1113/szbank"

@interface WDDFinancialTableViewCell()
{

    CGFloat frameWidth;/**  自身宽度 */
    CGFloat frameHeight;/**  自身高度 */
    NSMutableDictionary *newsDictionary;/**  重要新闻数据字典 */
    NSMutableDictionary *productsDictionary;/**  热推产品数据字典 */
    NSMutableDictionary *noticeDictionary;/**  最新公告数据字典 */
    NSMutableDictionary *introduceDictionary;/**  介绍数据字典 */
    NSMutableDictionary *standardDictionary;/**  规格数据字典 */
    
    
}

@property (nonatomic,strong) WDDFlipView *flipView;

@property (nonatomic,strong) WDDProductFlipView *productFlipView;
@end
@implementation WDDFinancialTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.financialOfLine];
        [self addSubview:self.financialOfLogo];
        [self addSubview:self.directionImage];
        [self addSubview:self.financialOfTitle];
        /**  初始化 */
        newsDictionary = [NSMutableDictionary dictionary];
        noticeDictionary = [NSMutableDictionary dictionary];
        productsDictionary = [NSMutableDictionary dictionary];
        introduceDictionary = [NSMutableDictionary dictionary];
        standardDictionary = [NSMutableDictionary dictionary];

        [self getNetWorkingNews];
        [self getNetWorkingProducts];
        [self getNetWorkingNotice];
    }
    return self;
}
/**  重要新闻数据请求 */
- (void)getNetWorkingNews{
    NSMutableArray *imageArray = [NSMutableArray array];/**  图片数组 */
    NSMutableArray *titleArray = [NSMutableArray array];/**  标题数组 */
    NSMutableArray *contentArray = [NSMutableArray array];/**  内容数组 */
    [NetWorkingDataManager getProtocolTemplateWithSuccess:^(id sender) {

        [NetWorkingDataManager getProtocolTemplateUpdateWithProtocolid:[sender[0] objectForKey:@"id"] success:^(id sender) {

            NSInteger index = 0;
            for (NSDictionary *dic in sender) {
                NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
                dataDic = [dic objectForKey:@"protocolData"];
                
                if (index == 1 || index == 2 || index == 5 || index == 7 || index == 8) {
                    NSString *string = [IMAGEURL stringByAppendingString:[dataDic objectForKey:@"缩略图"][0]];
                    [imageArray addObject:string];
                    [titleArray addObject:[dataDic objectForKey:@"新闻标题"][0]];
                    [contentArray addObject:[dataDic objectForKey:@"新闻内容"][0]];
                    
                    if (index == 8) {
                        break;
                    }
                }
                index ++;
            }
            [newsDictionary setObject:imageArray forKey:@"image"];
            [newsDictionary setObject:titleArray forKey:@"title"];
            [newsDictionary setObject:contentArray forKey:@"content"];
        /**  重要新闻模块初始化 */
        self.newsScrollView = [self getPagesForNews:imageArray.count dictionary:newsDictionary];
        [self.newsView addSubview:self.newsScrollView];
        } failure:^(id sender) {
        }];
    } failure:^(id sender) {
    }];
    
}
/**  热推产品数据请求 */
- (void)getNetWorkingProducts{
    NSMutableArray *imageArray = [NSMutableArray array];/**  图片数组 */
    NSMutableArray *titleArray = [NSMutableArray array];/**  标题数组 */
    [NetWorkingDataManager getProtocolRecommendProductWithParam:nil success:^(id sender) {

        NSInteger index = 0;
        for (NSDictionary *dic in [sender objectForKey:@"datalist"]) {
            if (index == 0 || index == 9 || index == 10 || index == 11 || index == 12 || index == 13 || index == 16 || index == 19 || index == 21) {
                [titleArray addObject:[dic objectForKey:@"NAME"]];
                [imageArray addObject:[dic objectForKey:@"PICURL"]];
                if (index == 21) {
                    break;
                }
            }
            index ++;
        }
        [productsDictionary setObject:titleArray forKey:@"title"];
        [productsDictionary setObject:imageArray forKey:@"image"];
        self.productsScrollView = [self getPagesForProducts:titleArray.count dictionary:productsDictionary];
        [self.productsView addSubview:self.productsScrollView];
    } failure:nil];
}
/**  最新公告数据请求 */
- (void)getNetWorkingNotice{
    NSMutableArray *titleArray = [NSMutableArray array];/**  标题数组 */
    NSMutableArray *contentArray = [NSMutableArray array];/**  内容数组 */
    [NetWorkingDataManager getProtocolTemplateWithSuccess:^(id sender) {

        [NetWorkingDataManager getProtocolTemplateUpdateWithProtocolid:[sender[6] objectForKey:@"id"] success:^(id sender) {
            
            NSInteger index = 0;
            for (NSDictionary *dic in sender) {
                NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
                dataDic = [dic objectForKey:@"protocolData"];
                
                if (index == 0 || index == 1 || index == 3 || index == 7 || index == 8) {

                    [titleArray addObject:[dataDic objectForKey:@"公告名称"][0]];
                    [contentArray addObject:[dataDic objectForKey:@"公告内容"][0]];
                    
                    if (index == 8) {
                        break;
                    }
                }
                index ++;
            }
            [noticeDictionary setObject:titleArray forKey:@"title"];
            [noticeDictionary setObject:contentArray forKey:@"content"];
            [self.tableView reloadData];
        } failure:^(id sender) {
        }];
    } failure:^(id sender) {
    }];
    
}
/**  重要新闻滚动视图 */
- (UIScrollView *)getPagesForNews:(NSInteger)pages dictionary:(NSDictionary *)dictionary{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT * 0.3)];
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.contentSize = CGSizeMake(pages * KWIDTH * 0.327, KHEIGHT * 0.3);
    
    for (int i = 0; i < pages; i ++) {
        /**  通过地址获取网络图片 */
        UIImage *imageOfNews = [LocalDataManager getImageWithURL:[dictionary objectForKey:@"image"][i]];
        /**  滚动按钮 */
        UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(i * KWIDTH * 0.3, 0, KWIDTH * 0.28, KHEIGHT * 0.26)];
        [imageButton setBackgroundImage:imageOfNews forState:UIControlStateNormal];
        [imageButton addTarget:self action:@selector(action_newsButton:) forControlEvents:UIControlEventTouchUpInside];
        imageButton.tag = 1000 + i;
        [scrollView addSubview:imageButton];
        /**  滚动标题 */
        UILabel *imageTitle = [[UILabel alloc] initWithFrame:CGRectMake(i * KWIDTH * 0.3, KHEIGHT * 0.26, KWIDTH * 0.28, KHEIGHT * 0.04)];
        imageTitle.textAlignment = NSTextAlignmentCenter;
        imageTitle.backgroundColor = [UIColor clearColor];
        imageTitle.font = [UIFont systemFontOfSize:20];
        imageTitle.text = [dictionary objectForKey:@"title"][i];
        [scrollView addSubview:imageTitle];
    }
    return scrollView;
}
/**  热推产品滚动视图 */
- (UIScrollView *)getPagesForProducts:(NSInteger)pages dictionary:(NSDictionary *)dictionary{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT * 0.3)];
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.contentSize = CGSizeMake(pages * KWIDTH * 0.315, KHEIGHT * 0.3);
    
    for (int i = 0; i < pages; i ++) {
        /**  通过地址获取网络图片 */
        UIImage *imageOfProducts = [LocalDataManager getImageWithURL:[dictionary objectForKey:@"image"][i]];
        /**  滚动按钮 */
        UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(i * KWIDTH * 0.3, 0, KWIDTH * 0.28, KHEIGHT * 0.3)];
        [imageButton setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"新推产品_底图"] forState:UIControlStateNormal];
        [imageButton addTarget:self action:@selector(action_productButton:) forControlEvents:UIControlEventTouchUpInside];
        imageButton.tag = 1000 + i;
        /**  图片 */
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((i * KWIDTH * 0.3) + KWIDTH * 0.04, KHEIGHT * 0.06, KWIDTH * 0.2, KHEIGHT * 0.21)];
        imageView.image = imageOfProducts;
        /**  标题 */
        UILabel *imageTitle = [[UILabel alloc] initWithFrame:CGRectMake((i * KWIDTH * 0.3) + KWIDTH * 0.02, KHEIGHT * 0.01, KWIDTH * 0.24, KHEIGHT * 0.05)];
        imageTitle.font = [UIFont systemFontOfSize:25];
        NSString *string = [NSString stringWithFormat:@"%@%@%@",@"【",[dictionary objectForKey:@"title"][i],@"】"];
        imageTitle.text = string;
        imageTitle.textAlignment = NSTextAlignmentCenter;
        imageTitle.backgroundColor = [UIColor clearColor];
        
        [scrollView addSubview:imageButton];
        [scrollView addSubview:imageTitle];
        [scrollView addSubview:imageView];
    }
    return scrollView;
}
/**  按钮点击事件(重要新闻) */
- (void)action_newsButton:(UIButton*)sender{
    self.flipView.titleLabel.text = [newsDictionary objectForKey:@"title"][sender.tag - 1000];
    [self.flipView.webView loadHTMLString:[newsDictionary objectForKey:@"content"][sender.tag - 1000] baseURL:nil];
    [self flipAnimation];
}

/**  按钮点击事件(热推产品) */
- (void)action_productButton:(UIButton*)button{
    NSMutableArray *introduceArray = [NSMutableArray array];/**  介绍数组 */
    NSMutableArray *qualityArray = [NSMutableArray array];/**  成色数组 */
    NSMutableArray *numbersArray = [NSMutableArray array];/**  规格数组 */
    NSMutableArray *weightArray = [NSMutableArray array];/**  克重数组 */
    NSMutableArray *priceArray = [NSMutableArray array];/**  价格数组 */
    NSMutableArray *imageArray = [NSMutableArray array];/**  图片数组 */
    [NetWorkingDataManager getProtocolPreciousMetalsWithParam:nil success:^(id sender) {
        for (NSString *string in [productsDictionary objectForKey:@"title"]) {
            for (NSDictionary *dic in sender[@"datalist"]) {
                if ([string isEqualToString:[dic objectForKey:@"NAME"]]) {
                    for (id key in [dic allKeys]) {
                        if ([key isEqualToString:@"SUMMARY"]) {
                            [introduceArray addObject:[dic objectForKey:@"SUMMARY"]];
                            for (NSString *string1 in [dic objectForKey:@"MATERIAL"]){
                                [qualityArray addObject:string1];
                            }
                            [numbersArray addObject:[dic objectForKey:@"SPECIFICATION"]];
                            [weightArray addObject:[dic objectForKey:@"WEIGHT"]];
                            [priceArray addObject:[dic objectForKey:@"PRICE"]];
                        }
                    }
                }
            }
        }
        for (int i = 0; i < 3; i ++) {
            [qualityArray addObject:@""];
        }
        NSInteger index = 0;
        for (NSDictionary *dic in sender[@"datalist"]) {
            if (index == 4 || index == 6 || index == 11 || index == 12 || index == 14 || index == 15 || index == 16 || index == 17 || index == 18) {
                [imageArray addObject:[dic objectForKey:@"DETAILSPIC"]];
                
            }
            index ++;
        }
        NSArray *newArray = @[imageArray[4],imageArray[5],imageArray[6],imageArray[7],imageArray[8],imageArray[0],imageArray[3],imageArray[1],imageArray[2]];
        
        self.productFlipView.title = [productsDictionary objectForKey:@"title"][button.tag - 1000];
        self.productFlipView.introduceStr = introduceArray[button.tag - 1000];
        self.productFlipView.qualityStr = qualityArray[button.tag - 1000];
        self.productFlipView.numbersStr = numbersArray[button.tag - 1000];
        self.productFlipView.weightStr = weightArray[button.tag - 1000];
        self.productFlipView.priceStr = priceArray[button.tag - 1000];
        /**  滚动图片传值 */
        self.productFlipView.sourceArray = newArray[button.tag - 1000];
        
        UIImage *imageOfProducts = [[UIImage alloc] init];
        if (!((button.tag - 1000) == 5) && !((button.tag - 1000) == 6)) {
            imageOfProducts = [LocalDataManager getImageWithURL:newArray[button.tag - 1000][0]];
        }
        else{
            imageOfProducts = nil;
        }
        self.productFlipView.productImage.image = imageOfProducts;
        [self.productFlipView productReudce];
        [self productFlipAnimation];

    } failure:nil];
}

#pragma mark -- tableView代理方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NoticeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *array = [noticeDictionary objectForKey:@"title"];
    cell.noticeImage.image =  [LocalDataManager getImageWithSourceOfPath:@"最新公告_题目"];
    cell.noticeTitle.text = array[indexPath.row];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array = [noticeDictionary objectForKey:@"title"];
    return array.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [self.flipView.webView loadHTMLString:[noticeDictionary objectForKey:@"content"][indexPath.row] baseURL:nil];
    self.flipView.titleLabel.text = [noticeDictionary objectForKey:@"title"][indexPath.row];
    [self flipAnimation];
}

/**  新闻与公告弹出框动画 */
- (void)flipAnimation{
    [self.flipView addSubview:self.flipView.webView];
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self.flipView];
    self.flipView.alpha = 0;
    /**  弹出动画 */
    [UIView animateWithDuration:1 animations:^{
        self.flipView.alpha = 1;
    } completion:nil];
}
/**  热推产品弹出框动画 */
- (void)productFlipAnimation{

    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self.productFlipView];
    self.productFlipView.alpha = 0;
    /**  弹出动画 */
    [UIView animateWithDuration:1 animations:^{
        self.productFlipView.alpha = 1;
    } completion:nil];
}

- (UIView *)newsView{
    if (!_newsView) {
        _newsView = [[UIView alloc] initWithFrame:CGRectMake(0, KHEIGHT * 0.07, KWIDTH, KHEIGHT * 0.3)];
        _newsView.backgroundColor = [UIColor clearColor];
    }
    return _newsView;
}

- (UIView *)productsView{
    if (!_productsView) {
        _productsView = [[UIView alloc] initWithFrame:CGRectMake(0, KHEIGHT * 0.07, KWIDTH, KHEIGHT * 0.3)];
        _productsView.backgroundColor = [UIColor clearColor];
    }
    return _productsView;
}

- (UIImageView *)financialOfLine {
    if (!_financialOfLine) {
        _financialOfLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, 1)];
        _financialOfLine.image = [UIImage imageNamed:@"金融资讯_线"];
    }
    return _financialOfLine;
}

- (UIImageView *)financialOfLogo {
    if (!_financialOfLogo) {
        _financialOfLogo = [[UIImageView alloc] initWithFrame:CGRectMake(KHEIGHT * 0.01, 20, KWIDTH * 0.02, KHEIGHT * 0.025)];
    }
    return _financialOfLogo;
}

- (UIImageView *)directionImage {
    if (!_directionImage) {
        _directionImage = [[UIImageView alloc] initWithFrame:CGRectMake(KWIDTH * 0.8, 20, KWIDTH * 0.03, KHEIGHT * 0.04)];
    }
    return _directionImage;
}


- (UILabel *)financialOfTitle {
    if (!_financialOfTitle) {
        _financialOfTitle = [[UILabel alloc] initWithFrame:CGRectMake(KHEIGHT * 0.05, 5, KWIDTH * 0.1, KHEIGHT * 0.05)];
        _financialOfTitle.font = [UIFont systemFontOfSize:22];
    }
    return _financialOfTitle;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, KHEIGHT * 0.07, KWIDTH * 0.9, KHEIGHT * 0.33)];
        _tableView.rowHeight = 70;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:[NoticeTableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}

- (WDDFlipView *)flipView{
    if (!_flipView) {
        _flipView = [[WDDFlipView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
    }
    return _flipView;
}

- (WDDProductFlipView *)productFlipView{
    if (!_productFlipView) {
        _productFlipView = [[WDDProductFlipView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
    }
    return _productFlipView;
}
@end
